package ru.resumeService.service.listener;

import org.h2.tools.RunScript;
import ru.resumeService.service.db.ConnectionFactory;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.sql.Connection;
import java.sql.SQLException;

public class InitDBContextListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {

        try {
            Connection connection = ConnectionFactory.getFactory().getConnection();
            RunScript.execute(connection, new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream("createDB.sql"), Charset.forName("UTF-8")));
        } catch (SQLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        //To change body of implemented methods use File | Settings | File Templates.
    }
}
